{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 矩阵加法\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65712911-cb84-41fe-a87a-3b3705b4976f",
   "metadata": {},
   "source": [
    "该代码定义了两个矩阵$A$和$B$，并计算它们的逐元素和。矩阵逐元素相加的原理是对矩阵中的每个对应位置的元素执行加法运算。\n",
    "\n",
    "1. **定义矩阵**：矩阵$A$和$B$分别定义如下：\n",
    "\n",
    "   $$\n",
    "   A = \\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "   $$\n",
    "   B = \\begin{bmatrix} 1 & 0 & 0 \\\\ 0 & 1 & 0 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "2. **逐元素相加**：代码通过双重列表推导式进行逐元素相加。对于矩阵中每个位置的元素$A[i][j]$和$B[i][j]$，计算它们的和并生成新矩阵`A_plus_B`：\n",
    "\n",
    "   $$\n",
    "   A\\_plus\\_B = \\begin{bmatrix} A[0][0] + B[0][0] & A[0][1] + B[0][1] & A[0][2] + B[0][2] \\\\ A[1][0] + B[1][0] & A[1][1] + B[1][1] & A[1][2] + B[1][2] \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "   计算后的结果为：\n",
    "\n",
    "   $$\n",
    "   A\\_plus\\_B = \\begin{bmatrix} 2 & 2 & 3 \\\\ 4 & 6 & 6 \\end{bmatrix}\n",
    "   $$\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94ecb0d9-5de2-4517-9a1f-74f34cdea28d",
   "metadata": {},
   "source": [
    "## 定义两个矩阵并计算其逐元素相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eba98b97-8d2e-4fc1-8456-1d51b279b9af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3], [4, 5, 6]]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [[1, 2, 3],  # 定义矩阵A\n",
    "     [4, 5, 6]]\n",
    "A  # 输出矩阵A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a8af49f4-0133-4f00-a5c9-eb95abe107dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 0, 0], [0, 1, 0]]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = [[1, 0, 0],  # 定义矩阵B\n",
    "     [0, 1, 0]]\n",
    "B  # 输出矩阵B"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9c9eb31-9fa6-4dd4-ac61-46932ea0209a",
   "metadata": {},
   "source": [
    "## 逐元素相加矩阵A和矩阵B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fdbcfbcd-370d-45b0-968d-37149750e8f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "A_plus_B = [[A[i][j] + B[i][j]  # 计算矩阵A和B中对应元素的和\n",
    "            for j in range(len(A[0]))]  # 遍历矩阵列\n",
    "            for i in range(len(A))]  # 遍历矩阵行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ba49388a-3a8e-4fde-9f02-4ea8ad87187f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2, 2, 3], [4, 6, 6]]\n"
     ]
    }
   ],
   "source": [
    "print(A_plus_B)  # 输出矩阵A和B逐元素相加的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
